package com.feiyu.sys.web

import com.feiyu.sys.service.LoginUser
import com.feiyu.sys.service.LoginUserService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*

import java.util.*

/**
 * Created by zdy on 2017/11/24.
 */
@RestController
@RequestMapping(value = "/users")
class LoginUserController {

    @Autowired
    LoginUserService userService

    /**
     * 取得列表
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    List<LoginUser> getUserList() {

        return userService.getList()
    }

    @RequestMapping(value = "/", method = RequestMethod.POST)
    String postUser(@ModelAttribute LoginUser user) {
        return userService.addUser(user) > 0 ? "success" : "failure"
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    LoginUser getUser(@PathVariable Long id) {
        // 处理"/users/{id}"的GET请求，用来获取url中id值的User信息
        // url中的id可通过@PathVariable绑定到函数的参数中
        return users.get(id)
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    String putUser(@PathVariable Long id, @ModelAttribute LoginUser user) {
        // 处理"/users/{id}"的PUT请求，用来更新User信息
        LoginUser u = users.get(id)
        u.setName(user.getName())
        u.setAge(user.getAge())
        users.put(id, u)
        return "success"
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    String deleteUser(@PathVariable Long id) {
        // 处理"/users/{id}"的DELETE请求，用来删除User
        users.remove(id)
        return "success"
    }
}
